using System;
using System.Linq;
using System.Management.Automation;
using System.Reflection;
using System.Security;
using System.Text;
using RabbitMQ.Client;

namespace PoshRabbit
{
    public abstract class RabbitCmdletBase : PSCmdlet, IRabbitMQConnection
    {
        private readonly RabbitConnector _connector;

        protected RabbitCmdletBase()
        {
            _connector = new RabbitConnector(this);
            ExchangeType = RabbitMQ.Client.ExchangeType.Topic;
            Protocol = "AMQP_0_8";
        }

        [Parameter(
            HelpMessage = "The name of the queue to create; if unspecified an anonymous queue will be created by the rabbit mq server",
            Mandatory = true,
            ValueFromPipelineByPropertyName = true
            )]
        [Alias("X")]
        [ValidateNotNullOrEmpty]
        public string Exchange{ get; set; }

        [Parameter(
            HelpMessage = "The name of the queue to create; if unspecified an anonymous queue will be created by the rabbit mq server",
            Mandatory = false,
            ValueFromPipelineByPropertyName = true
            )]
        [Alias("T")]
        [ValidateNotNullOrEmpty]
        public string ExchangeType { get; set; }

        [Parameter(
            HelpMessage = "The name of the queue to create; if unspecified an anonymous queue will be created by the rabbit mq server",
            Mandatory = true,
            ValueFromPipelineByPropertyName = true
            )]
        [Alias("RK", "Key", "Filter")]
        [ValidateNotNullOrEmpty]
        public string RoutingKey { get; set; }

        [Parameter(
            HelpMessage = "The name of the queue to create; if unspecified an anonymous queue will be created by the rabbit mq server",
            Mandatory = false,
            ValueFromPipelineByPropertyName = true
            )]
        [Alias("VHost", "VH")]
        public string VirtualHost { get; set; }

        [Parameter(
            HelpMessage = "The name of the queue to create; if unspecified an anonymous queue will be created by the rabbit mq server",
            Mandatory = true,
            ValueFromPipelineByPropertyName = true
            )]
        [Alias("Host","Server","Svr")]
        [ValidateNotNullOrEmpty]
        public string HostName { get; set; }

        [Parameter(
            HelpMessage = "The name of the queue to create; if unspecified an anonymous queue will be created by the rabbit mq server",
            Mandatory = false,
            ValueFromPipelineByPropertyName = true
            )]
        [Alias("Usr")]
        public string UserName { get; set; }

        [Parameter(
            HelpMessage = "The name of the queue to create; if unspecified an anonymous queue will be created by the rabbit mq server",
            Mandatory = false,
            ValueFromPipelineByPropertyName = true
            )]
        [Alias("Pwd")]
        public SecureString Password{ get; set; }

        [Parameter(
            HelpMessage = "The name of the queue to create; if unspecified an anonymous queue will be created by the rabbit mq server",
            Mandatory = false,
            ValueFromPipelineByPropertyName = true
            )]
        [Alias("pcl")]
        public string Protocol { get; set; }

        [Parameter(Mandatory = false)]
        [ValidateNotNullOrEmpty]
        public string Encoding { get; set; }

        protected Encoding MessageEncoding
        {
            get
            {
                return Connector.MessageEncoding;
            }
        }


        protected void WriteDebugFormat( string f, params object[] args)
        {
            var s = String.Format(f, args);
            WriteDebug( s );
        }

        protected void CreateModel()
        {
            Connector.CreateModel();
        }

        protected void DestroyModel()
        {
            Connector.DestroyModel();
        }

        protected IModel Model { get { return Connector.Model; } }
        protected IConnection Connection { get { return Connector.Connection; } }

        protected RabbitConnector Connector
        {
            get { return _connector; }
        }
    }
}